Blog

Curt Gratz

March 19, 2010

Spread the word


Share your thoughts

MockBox can have many amazing uses.  One well documented use is in assistance in creating complicated unit tests insuring you are only testing one small unit of work, however it can be used for many other interesting use cases.

One of the ways we find MockBox useful during our development cycle at Computer Know How is to mock objects we haven't had the time to complete yet, but we do know what we expect as response.  This allows us to continue development without waiting for the piece of something we haven't done yet slow us down, but keep the method calls exactly as they will be in when the object is complete.

So, how do you use MockBox outside of the context of a unit test?  Well, its easy.  

Inside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet inside a ColdBox app mockBox = createObject("component","coldbox.system.testing.MockBox").init();

Outside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet outside a ColdBox app. mockBox = createObject("component","mockbox.system.testing.MockBox").init();

Now that MockBox is initialized, we can start mocking objects. Lets say we have a User Object that we haven't had time to build yet. Right now it looks really advanced with lots of cool properties and functions that we spent a ton of time on. Something like this.

cfcomponent hint="I am the User ORM cfc" /cfcomponent

Now what if we needed to build a Welcome screen based off this User Object. Well, we could do something like this.

var rc = event.getCollection(); //get the users name, later we will need to switch this to get it from the user object. rc.Name = "Paul Was Saul"; But later, we would have to implement our user object once we are done with it. But what if we mocked our user object with the getName method it will ultimately have. var rc = event.getCollection(); //get an instance of mockbox var mockBox = createObject("component","coldbox.system.testing.MockBox").init() //Mock a User Object since its not built yet. var User=mockBox.createMock(className="model.ORM.User",clearMethods=true,callLogging=true); User.$(method="getName",returns="Paul Was Saul"); //get the users name rc.Name = User.getName();

Now, I know what your thinking. The non-mocked version looks a lot simpler then the mocked version. Well that is probably true in this simple example, but use your imagination and expand this example using the power of your mind and you can see how handy MockBox can be in your own development.

Want to learn more about MockBox. Read the wiki. As with everything done by Luis Majano, the documentation is outstanding.

http://wiki.coldbox.org/wiki/MockBox.cfm

Or Download it at

http://www.coldbox.org/download

Yet another tool to put in your toolbox to make your life simpler brought to you by Team ColdBox.

Add Your Comment

(2)

Mar 19, 2010 10:53:11 UTC

by Luis Majano

Cool approach Curt, My head started spinning and could be cool to have a mock interceptor that sets up all the mocks you would need for your session until objects get build out. This way you can put all your mock code in this single object and be coding it out. Very interesting approach. PS: you don't need to do: rc = event.getCollection() on your views/layouts. That's done implicitly :)

Mar 19, 2010 10:58:59 UTC

by Curt Gratz

Thanks man. In reality I do it in the mocks preHandler interceptor so its available to me when I need it. PS: those are are meant to be handlers, I don't think it does it implicitly there.

Recent Entries

Into the Box 2024: Your Gateway to the Future of Tech!

Into the Box 2024: Your Gateway to the Future of Tech!

Are you ready to advance your coding skills? The future of Modern Web Development awaits at Into the Box 2024, and we're thrilled to announce that due to high demand, we're extending our Early Bird pricing for an additional week!

Maria Jose Herrera
Maria Jose Herrera
April 26, 2024
Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024